package com.chis.shardingjdbc.main;

import com.chis.shardingjdbc.beans.TdJxcStmtzx;
import com.chis.shardingjdbc.dao.ShardjdbcDao;
import com.chis.util.RandomGUID;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wlj
 * @version 2019年05月21日
 * @Description
 * @Company zwx
 */
public class Testsharding {
    public static void main(String[] args) {
        try {
            // 建分表，表名要求小写， 另外事务暂未研究

            // 测试插入
//            testInsert();
            // 测试更新
//            testUpdate();
            // 测试查询
//             testSelect2();
            // 测试普通表
//            test();
            // 数据搬运
//             datatrans();
            test1();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void test1() throws Exception{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml", "spring-shardingjdbc.xml", "spring-mybatis.xml", "spring-beans.xml"});
        context.start();
        ShardjdbcDao shardjdbcDao = (ShardjdbcDao) context.getBean("shardjdbcDao");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Long l1 = System.currentTimeMillis();
        shardjdbcDao.selectTz3(simpleDateFormat.parse("2020-11-1"),simpleDateFormat.parse("2020-11-1"));
        Long l2 = System.currentTimeMillis();
        System.out.println("首次耗时"+(l2-l1));

        shardjdbcDao.selectTz3(simpleDateFormat.parse("2020-10-1"),simpleDateFormat.parse("2020-10-30"));
        Long l3 = System.currentTimeMillis();
        System.out.println("1月耗时:"+(l3-l2));

        shardjdbcDao.selectTz3(simpleDateFormat.parse("2020-10-1"),simpleDateFormat.parse("2020-12-30"));
        Long l4 = System.currentTimeMillis();
        System.out.println("3月耗时:"+(l4-l3));

        shardjdbcDao.selectTz3(simpleDateFormat.parse("2020-7-1"),simpleDateFormat.parse("2020-12-30"));
        Long l5 = System.currentTimeMillis();
        System.out.println("6月耗时:"+(l5-l4));

//        Object object = shardjdbcDao.selectTz3(simpleDateFormat.parse("2019-1-1"),simpleDateFormat.parse("2019-2-1"));
//        System.out.println(object);
//        System.out.println(" 耗时1："+(System.currentTimeMillis()-l1));
//        Long l2 = System.currentTimeMillis();
//        shardjdbcDao.selectTz3(simpleDateFormat.parse("2019-1-1"),simpleDateFormat.parse("2019-2-1"));
//        System.out.println(" 耗时2："+(System.currentTimeMillis()-l2));

//        shardjdbcDao.selectTz3(simpleDateFormat.parse("2020-1-1"),simpleDateFormat.parse("2020-9-25"));
//        System.out.println(object);
//        System.out.println("12月耗时："+(System.currentTimeMillis()-l5));
    }



    public static void test() throws Exception{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml", "spring-shardingjdbc.xml", "spring-mybatis.xml", "spring-beans.xml"});
        context.start();
        ShardjdbcDao shardjdbcDao = (ShardjdbcDao) context.getBean("shardjdbcDao");
        List<Map<Object,Object>> list = shardjdbcDao.test();
        System.out.println(1);

    }

    /**
     * 日期区间查询
     * @throws Exception
     */
    public static void testSelect2() throws Exception{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml", "spring-shardingjdbc.xml", "spring-mybatis.xml", "spring-beans.xml"});
        context.start();
        ShardjdbcDao shardjdbcDao = (ShardjdbcDao) context.getBean("shardjdbcDao");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Long l1 = System.currentTimeMillis();
        shardjdbcDao.selectTz2(simpleDateFormat.parse("2020-5-1"),simpleDateFormat.parse("2020-5-30"));
        System.out.println(" 耗时1："+(System.currentTimeMillis()-l1));
//        Long l2 = System.currentTimeMillis();
//        shardjdbcDao.selectTz2(simpleDateFormat.parse("2020-3-1"),simpleDateFormat.parse("2020-5-30"));
//        System.out.println(" 耗时2："+(System.currentTimeMillis()-l2));
    }

    /**
     * 日期精确查询
     * @throws Exception
     */
    public static void testSelect() throws Exception{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml", "spring-shardingjdbc.xml", "spring-mybatis.xml", "spring-beans.xml"});
        context.start();
        ShardjdbcDao shardjdbcDao = (ShardjdbcDao) context.getBean("shardjdbcDao");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<TdJxcStmtzx> list = shardjdbcDao.selectTz(simpleDateFormat.parse("2020-7-23"));
        System.out.println(1);
    }


    /**
     * 更新，查询条件必须有日期
     * @throws Exception
     */
    public static void testUpdate() throws Exception{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml", "spring-shardingjdbc.xml", "spring-mybatis.xml", "spring-beans.xml"});
        context.start();
        ShardjdbcDao shardjdbcDao = (ShardjdbcDao) context.getBean("shardjdbcDao");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        shardjdbcDao.updateTz("1",simpleDateFormat.parse("2020-7-20"));
    }


    /**
     * 新增，按发生日期自动分表
     * @throws Exception
     */
    public static void testInsert() throws Exception{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml", "spring-shardingjdbc.xml", "spring-mybatis.xml", "spring-beans.xml"});
        context.start();
        ShardjdbcDao shardjdbcDao = (ShardjdbcDao) context.getBean("shardjdbcDao");
        //新增,按发生日期
        List<TdJxcStmtzx> list = new ArrayList<>();
        TdJxcStmtzx tdJxcStmtzx = new TdJxcStmtzx();
        tdJxcStmtzx.setGuid(new RandomGUID().toString());
        tdJxcStmtzx.setUnitCode(1L);
        tdJxcStmtzx.setPurchaseUnit(1L);
        tdJxcStmtzx.setConUnitCode(1L);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        tdJxcStmtzx.setHpnDate(simpleDateFormat.parse("2020-6-20"));
        tdJxcStmtzx.setInoutTag(1);
        tdJxcStmtzx.setOpTag(1);
        tdJxcStmtzx.setPrc(BigDecimal.ONE);
        tdJxcStmtzx.setCntstmId("1");
        tdJxcStmtzx.setSubId("1");
        tdJxcStmtzx.setQty(1);
        tdJxcStmtzx.setCreateDate(new Date());
        tdJxcStmtzx.setModifyDate(new Date());
        tdJxcStmtzx.setCreateManCode(1L);
        tdJxcStmtzx.setModifyManCode(1L);
        tdJxcStmtzx.setDelmark(0);
        list.add(tdJxcStmtzx);
        tdJxcStmtzx = new TdJxcStmtzx();
        tdJxcStmtzx.setGuid(new RandomGUID().toString());
        tdJxcStmtzx.setUnitCode(1L);
        tdJxcStmtzx.setPurchaseUnit(1L);
        tdJxcStmtzx.setConUnitCode(1L);
        tdJxcStmtzx.setHpnDate(simpleDateFormat.parse("2020-7-20"));
        tdJxcStmtzx.setInoutTag(1);
        tdJxcStmtzx.setOpTag(1);
        tdJxcStmtzx.setPrc(BigDecimal.ONE);
        tdJxcStmtzx.setCntstmId("1");
        tdJxcStmtzx.setSubId("1");
        tdJxcStmtzx.setQty(1);
        tdJxcStmtzx.setCreateDate(new Date());
        tdJxcStmtzx.setModifyDate(new Date());
        tdJxcStmtzx.setCreateManCode(1L);
        tdJxcStmtzx.setModifyManCode(1L);
        tdJxcStmtzx.setDelmark(0);
        list.add(tdJxcStmtzx);
        shardjdbcDao.saveTz(list);
    }
}
